/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.groupg.practice;

import de.agilecoders.wicket.webjars.request.resource.WebjarsCssResourceReference;
import org.apache.wicket.markup.head.CssHeaderItem;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import java.io.InputStream;
import java.io.Serial;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class HomePage extends WebPage {
    @Serial
    private static final long serialVersionUID = 1L;


    @Override
    public void renderHead(IHeaderResponse response) {
        super.renderHead(response);
//        getRequestCycle()
        response.render(CssHeaderItem.forReference(
                new WebjarsCssResourceReference("picocss__pico/2.0.6/css/pico.min.css")));
    }

    public HomePage(final PageParameters parameters) {
        super(parameters);

        List<String[]> countries = loadCountriesFromCsv();
        ListDataProvider<String[]> listDataProvider =
                new ListDataProvider<String[]>(countries);

        DataView<String[]> dataView = new DataView<String[]>("rows", listDataProvider) {

            @Override
            protected void populateItem(Item<String[]> item) {
                String[] countriesArr = item.getModelObject();
                RepeatingView repeatingView = new RepeatingView("dataRow");

                for (String s : countriesArr) {
                    repeatingView.add(
                            new Label(repeatingView.newChildId(), s));
                }
                item.add(repeatingView);
            }
        };

        dataView.setItemsPerPage(15);

        add(dataView);
        add(new PagingNavigator("pagingNavigator", dataView));
    }

    private List<String[]> loadCountriesFromCsv() {
        InputStream countriesStream = HomePage.class.getResourceAsStream("countries.csv");
        assert countriesStream != null;
        Scanner scanner = new Scanner(countriesStream);
        List<String[]> countries = new ArrayList<String[]>();

        while (scanner.hasNext()) {
            String curLine = scanner.nextLine();
            String[] countryData = curLine.split(";");

            countries.add(countryData);
        }

        scanner.close();

        return countries;
    }
}
